capture log close
log using C:\data\UI_2020\logs\regs_basic_rev3_ba2.log, replace
*************************************************************************
* Created by: 	Rob Valletta						
* Created on: 	05/15/2022
* Description: 	Regs of transition rates on controls, UI replacement rates.
*		UI rep rates calculated at individual level using CPS ASEC 
*		and Ganong et al. calculator.
*		Regressions used matched panel for Jan.-Jul 2020
*		(restricted to those with ASEC info)
* Modified on:	Based on earlier "regs_paper_5lw" (use longitudinal weights)
*		Include EU and EN regs, other small changes (commented)
*		6/6/22: Include majocc dummies to this version "2"
* 		6/18/22: Use new "v2" data set (small obs increase)
*		2/3/23: Exclude armed forces
*		2/20/23: "ba" version does simple before/after regression
*		3/6/23: Import new state unemp/emp data, make sure matched to base month (t-1, not t
*************************************************************************

version 15
clear *
set more off
capture program drop _all
cd C:\data\UI_2020
set linesize 120

** Pull in analysis extract 
use data\CPS_2020_unbalanced_v2.dta, clear

** (DROP THIS STEP HERE) Reset month to match forward month (through Jul 2020)
** replace month=month+1

* FORM UI REPLACEMENT RATE (WITH MISSINGS), DROP 0'S and DC
gen UI_rep=(UI_CARES/weekly_earnings)
  replace UI_rep=0 if UI_rep==.
drop if UI_rep==0
drop if st=="DC"

** DEFINE SOME MACROS
macro define dur_FV i.durmoncat
macro define dur_JR dur dur_sq dur_inv new_un gte26
macro define X_p i.educ i.agecat female married fem_marr i.racegrp
macro define X_s urate* d3lne*

egen double pid=group(peridnum)
xtset pid mis
gen pwlgwgt_b=f.pwlgwgt if matchf==1
  replace pwlgwgt=pwlgwgt_b

** FORM CORRECTED MAJOCC
gen majocc2=majocc
  replace majocc2=l.majocc2 if (majocc<0 & lfstat2==2 & l.lfstat2==2)

** SET NEG MAJIND AND MAJOCC TO MISSING
replace majind=. if majind<=0
replace majind2=. if majind2<=0
replace majocc=. if majocc<=0
replace majocc2=. if majocc2<=0

** DROP FLAGGED BAD MATCHES
drop if fl_bad_cps==1

** DROP ARMED FORCES
drop if (majind==14 | majocc==11)

** Drop state vars, create year var for merge
drop emp-d12lne urate*
gen year=2020

** Update and re-match: CURRENT MONTH
  ** (base month for transition)
sort year month state
merge m:1 year month state using data\emp_urate_lf_state, keep(1 3) nogen

** NOW RE-SET MONTH (forward transtions identified by outcome month)
replace month=month+1

** For pre/post design, interact with month group ("post")
gen UI_rep_post=UI_rep*(month>=4)

** Form urate and d3lne cubics
gen urate2=urate^2
gen urate3=urate^3
gen d3lne2=d3lne^2
gen d3lne3=d3lne^3

** REGRESSIONS HERE (some sample changes)
preserve

** UE unrestricted
keep if lfstat==2
logit UEf UI_rep* i.month $dur_FV $X_p $X_s i.majind i.majocc i.state [iw=pwlgwgt], vce(robust)
margins, dydx(UI_rep*) post
eststo UE_1f

** EU unrestricted (drop dur)
restore, preserve
keep if lfstat==1
logit EUf UI_rep* i.month $X_p $X_s i.majind i.majocc i.state [iw=pwlgwgt], vce(robust)
margins, dydx(UI_rep*) post
eststo EU_1f

** EN unrestricted (drop dur)
restore, preserve
keep if lfstat==1
logit ENf UI_rep* i.month $X_p $X_s i.majind i.majocc i.state [iw=pwlgwgt], vce(robust)
margins, dydx(UI_rep*) post
eststo EN_1f

** UE match2f
  ** SLIGHT CHANGE FROM PREVIOUS: Included "restore, preserve" here to account for lfstat/lfstat2 diffs (as in next reg)
restore, preserve
keep if lfstat2==2 & match2f==1
logit exit2_emp UI_rep* i.month $dur_FV $X_p $X_s i.majind2 i.majocc2 i.state [iw=pwlgwgt], vce(robust)
margins, dydx(UI_rep*) post
eststo UE_2f
  ** Check monthly sample count
tab month

** UN
restore, preserve
keep if lfstat2==2 & match2f==1
logit exit2_nilf UI_rep* i.month $dur_FV $X_p $X_s i.majind2 i.majocc2 i.state [iw=pwlgwgt], vce(robust)
margins, dydx(UI_rep*) post
eststo UN_2f

** NE (drop dur, majind2)
restore, preserve
keep if N==1
logit NEf UI_rep* i.month $X_p $X_s i.state [iw=pwlgwgt], vce(robust)
margins, dydx(UI_rep*) post
eststo NE_1f

** Balanced sample
** Pull in analysis extract 
use data\CPS_2020_balanced_v2.dta, clear

** Reset month to match forward month (through Aug 2020)
replace month=month+1

* FORM UI REPLACEMENT RATE (WITH MISSINGS), DROP 0'S and DC
gen UI_rep=(UI_CARES/weekly_earnings)
  replace UI_rep=0 if UI_rep==.
  drop if UI_rep==0
drop if st=="DC"

** DEFINE SOME MACROS
** Drop "i.racegrp" for now (var not in data)
macro define dur_FV i.durmoncat
macro define dur_JR dur dur_sq dur_inv new_un gte26
macro define X_p i.educ i.agecat female married fem_marr i.racegrp
macro define X_s urate* d3lne*

egen double pid=group(peridnum)
xtset pid mis
gen pwlgwgt_b=f.pwlgwgt if matchf==1
  replace pwlgwgt=pwlgwgt_b

** FORM CORRECTED MAJOCC
gen majocc2=majocc
  replace majocc2=l.majocc2 if (majocc<0 & lfstat2==2 & l.lfstat2==2)

** SET NEG MAJIND AND MAJOCC TO MISSING
replace majind=. if majind<=0
replace majind2=. if majind2<=0
replace majocc=. if majocc<=0
replace majocc2=. if majocc2<=0

** DROP FLAGGED BAD MATCHES
drop if fl_bad_cps==1

** DROP ARMED FORCES
drop if (majind==14 | majocc==11)

** Drop state vars, create year var for merge
drop emp-d12lne urate*
gen year=2020

** Update and re-match: CURRENT MONTH
  ** (base month for transition)
sort year month state
merge m:1 year month state using data\emp_urate_lf_state, keep(1 3) nogen

** NOW RE-SET MONTH (forward transtions identified by outcome month)
replace month=month+1

** For pre/post design, interact with month group ("post")
gen UI_rep_post=UI_rep*(month>=4)

** Form urate and d3lne cubics
gen urate2=urate^2
gen urate3=urate^3
gen d3lne2=d3lne^2
gen d3lne3=d3lne^3

** Balanced sample reg
keep if lfstat2==2 & match2f==1
logit exit2_emp UI_rep* i.month $dur_FV $X_p $X_s i.majind2 i.majocc2 i.state [iw=pwlgwgt], vce(robust)
margins, dydx(UI_rep*) post
eststo UE_ba

** Export results using estout
#delimit ;
esttab UE_2f UE_1f UE_ba UN_2f NE_1f EU_1f EN_1f using output\regs_basic_rev3_ba2, 
csv keep(UI_rep*) cells(b(vacant(-) star fmt(3)) se(fmt(3) par)) label
mlabels(dep) collabels(none) eqlabels(none)  
starlevels(* 0.10 ** 0.05 *** .01) replace;
#delimit cr

#d cr

log close
